<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected function _initDb() {
		
		try {

			$dbOption = $this->getOption('resources');
	    	$dbOption = $dbOption['db'];
	//    	zend_debug::dump($dbOption);
	        // Setup database
	        $db = Zend_Db::factory($dbOption['adapter'],$dbOption['params']);
	        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
	        $db->query("SET NAMES 'utf8'");
	        $db->query("SET CHARACTER SET 'utf8'");       

	        Zend_Registry::set('connectDB',$db);
	        //Khi thiet lap che do nay model moi co the su dung duoc
	        Zend_Db_Table::setDefaultAdapter($db);
	        Zend_Db_Table_Abstract::setDefaultAdapter($db);
	        
	                //Caching
			$frontendOptions = array(
			      'lifetime'     => 25200,
			         'automatic_serialization' => true
			         );
			$backendOptions  = array(
			          'cache_dir'                => APPLICATION_PATH . '/tmp'
			         );
			$cache = Zend_Cache::factory(
			      'Core',
			                     'File',
			                     $frontendOptions,
			                     $backendOptions
			                     );
			//Cache table metadata
			Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
	        // Return it, so that it can be stored by the bootstrap
	        return $db;
			
		} catch (Exception $e) {
			echo "Hệ thống đang nâng cấp, xin mời bạn trở lại sau.";
		}
		
	}
	protected function _initSetConstants(){
		$config = parse_ini_file(APPLICATION_PATH . '/configs/constant.ini');
		foreach ($config as $key => $value){
            if(!defined($key)){
                define($key, $value);
            }
		}
	}
 	protected function _initController ( )
    {
    	try {

    		$front = Zend_Controller_Front::getInstance();
	        $front->setControllerDirectory( array (
	        	'admin' => APPLICATION_PATH . '/modules/Admin/controllers',
	        	'wap' => APPLICATION_PATH . '/modules/Wap/controllers',
	        	'verify' => APPLICATION_PATH . '/modules/Verify/controllers',
	        ));
	        return $front;
    		
    	} catch (Exception $e) {
    		echo "Hệ thống đang nâng cấp, xin mời bạn trở lại sau.";
    	}
        
    }

    protected function _initLoadRouter()
	{
		try {
			$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/router.ini','thietlap');
			$objRouter = new Zend_Controller_Router_Rewrite();
			//new Zend_Controller_Router_Route_Regex()
			$router = $objRouter->addConfig($config,'routers');
			
			$front = Zend_Controller_Front::getInstance();
			$front->setRouter($router);
		} catch (Exception $e) {
			echo "Hệ thống đang nâng cấp, xin mời bạn trở lại sau."
;		}
		
	}
    
//	protected function _initMail() {
//		$mailOption = $this->getOption('resources');
//		$mailOption = $mailOption['mail'];
//		$host = $mailOption['host'];
//		$mailOption = $mailOption['info'];
//    	
//    	$transport = new Zend_Mail_Transport_Smtp($host, $mailOption);
//    	Zend_Registry::set('mailTransport',$transport);
//    	Zend_Mail::setDefaultTransport ( $transport );
//	}
}

